// LSL script generated: RLV-New.RLV_MENU.DV_MAIN_CAM.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
list gMenu = [];
list statesMenu = [0,0,0,0];
key ToucherID;
integer channel_dialog;
integer listen_id;
string msg;
float gdistance;
list distance = ["2","5","10","15","20","30","40"];
integer crossing = 0;
displayMenu(){
    (gMenu = []);
    if ((llList2Integer(statesMenu,0) == 1)) (gMenu += "[x]Range");
    else  (gMenu += "[ ]Range");
    (gMenu += "Back");
    (gMenu += distance);
    (msg = "Pick one");
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,gMenu,channel_dialog);
}

llRlvSay(string command){
    {
        llOwnerSay(("@" + command));
    }
}

RequestPerms(){
    if (llGetAttached()) {
        llRequestPermissions(llGetOwner(),1024);
    }
}

makeRandom(){
    integer _rand;
    (_rand = ((integer)llFrand(99)));
    llRlvSay((("setenv_scenegamma:0." + ((string)_rand)) + "=force"));
    (_rand = ((integer)llFrand(99)));
    llRlvSay((("setenv_ambienti:0." + ((string)_rand)) + "=force"));
    (_rand = ((integer)llFrand(99)));
    llRlvSay((("setenv_ambientr:0." + ((string)_rand)) + "=force"));
    (_rand = ((integer)llFrand(99)));
    llRlvSay((("setenv_ambientg:0." + ((string)_rand)) + "=force"));
    (_rand = ((integer)llFrand(99)));
    llRlvSay((("setenv_ambientb:0." + ((string)_rand)) + "=force"));
}

makeDefault(){
    llRlvSay("setenv_scenegamma:1.0=force");
    llRlvSay("setenv_ambientr:0.34=force");
    llRlvSay("setenv_ambientg:0.35=force");
    llRlvSay("setenv_ambientb:0.35=force");
    llRlvSay("setenv_ambienti:0.35=force");
}

llLRP(integer element_new,integer placeinlist){
    (statesMenu = llListReplaceList(statesMenu,[element_new],placeinlist,placeinlist));
}

default {

    state_entry() {
        RequestPerms();
    }

     listen(integer channel,string name,key id,string message) {
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            if ((message == "[x]Range")) {
                llLRP(0,0);
                llSetTimerEvent(1.0);
                displayMenu();
            }
            else  if ((message == "[ ]Range")) {
                llLRP(1,0);
                llSetTimerEvent(1.0);
                displayMenu();
            }
            else  if ((message == "Back")) {
                llMessageLinked(-4,1337,"DV_MAIN_MISC",ToucherID);
            }
            else  if ((~llListFindList(distance,((list)message)))) {
                (gdistance = ((float)message));
                displayMenu();
            }
        }
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                displayMenu();
            }
        }
        if ((num == 1338)) {
            if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
            }
        }
    }

  	
  	on_rez(integer num) {
        RequestPerms();
    }

  	

  	
  	timer() {
        if ((llList2Integer(statesMenu,0) == 1)) {
            if ((llGetPermissions() & 1024)) {
                vector campos = llGetCameraPos();
                vector position = llGetPos();
                float between = llVecDist(campos,position);
                if ((between > gdistance)) {
                    llSetTimerEvent(1.0);
                    llRlvSay("setenv=n");
                    (crossing = 1);
                    makeRandom();
                }
                else  {
                    if ((crossing == 1)) {
                        llSetTimerEvent(1.0);
                        llRlvSay("setenv=y");
                        makeDefault();
                        (crossing = 0);
                    }
                }
            }
        }
        else  {
            if ((crossing == 1)) {
                llSetTimerEvent(0.0);
                llRlvSay("setenv=y");
                makeDefault();
                (crossing = 0);
            }
        }
    }
}
